/* menu.c

Copyright (C) Alex S. (mailto: md5sum@alexsp.ru)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details
http://www.gnu.org/licenses/gpl-2.0.html.

You should have received a copy of the GNU General Public License
along with this program in the file gpl-2.0.txt;
if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "menu.h"
#include "glcd.h"

menu_item_t menu_item=DRAW_MENU;

void menu(uint8_t key) {
	//	if ((key&(KEY_ENTER|KEY_PLUS|KEY_MINUS))==0) return;
	if (key==0) {draw_date(menu_item);return;}
	switch (menu_item) {
	case DRAW_MENU:
		GLCD_clear();
		m41_control=m41_get_control();
		if (m41_control==0) m41_control=0x20;
		menu_item=MENU_NONE;
		draw_date(menu_item);
		draw_time_adj(menu_item);
		draw_press_adj(menu_item);
		draw_lcd_max(menu_item);
		draw_lcd_min(menu_item);
		draw_lcd_time(menu_item);
		draw_exit(menu_item);
		break;
	case MENU_NONE:
		if (key==KEY_CHANGE) {
			menu_item=LED_MAX_SELECT;
			draw_lcd_max(menu_item);
		}
		break;
	case DAY_SELECT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			m41stop();
			menu_item=DAY_EDIT;
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=HOUR_SELECT;
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=EXIT;
			draw_exit(menu_item);
		}
		draw_date(menu_item);
		break;
	case HOUR_SELECT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			m41stop();
			menu_item=HOUR_EDIT;
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=TIME_ADJ_SELECT;
			draw_time_adj(menu_item);
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=DAY_SELECT;
		}
		draw_date(menu_item);
		break;
	case DAY_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=MONTH_EDIT;
		}
		if (key&KEY_PLUS) {
			if (time.day<31) time.day++; else time.day=1;
		}
		if (key&KEY_MINUS) {
			if (time.day>1) time.day--; else time.day=31;
		}
		draw_date(menu_item);
		break;
	case MONTH_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=YEAR_EDIT;
		}
		if (key&KEY_PLUS) {
			if (time.month<12) time.month++; else time.month=1;
		}
		if (key&KEY_MINUS) {
			if (time.month>1) time.month--; else time.month=12;
		}
		draw_date(menu_item);
		break;
	case YEAR_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			if (check_date()) {
				menu_item=DAY_EDIT;
				time.day=check_date();
			}else {menu_item=HOUR_EDIT;}
		}
		if (key&KEY_PLUS) {
			if (time.year<99) time.year++; else time.year=11;
		}
		if (key&KEY_MINUS) {
			if (time.year>11) time.year--; else time.year=99;
		}
		draw_date(menu_item);
		break;
	case HOUR_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=MINUTE_EDIT;
		}
		if (key&KEY_PLUS) {
			if (time.hour<23) time.hour++; else time.hour=0;
		}
		if (key&KEY_MINUS) {
			if (time.hour>0) time.hour--; else time.hour=23;
		}
		draw_date(menu_item);
		break;
	case MINUTE_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=TIME_ADJ_SELECT;
			m41set_time();
			draw_time_adj(menu_item);
		}
		if (key&KEY_PLUS) {
			if (time.min<59) time.min++; else time.min=0;
		}
		if (key&KEY_MINUS) {
			if (time.min>0) time.min--; else time.min=59;
		}
		draw_date(menu_item);
		break;
	case TIME_ADJ_SELECT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=TIME_ADJ_EDIT;
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=PRESS_ADJ_SELECT;
			draw_press_adj(menu_item);
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=HOUR_SELECT;
			draw_date(menu_item);
		}
		draw_time_adj(menu_item);
		break;
	case TIME_ADJ_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=PRESS_ADJ_SELECT;
			m41calibrate();
			draw_exit(menu_item);
		}
		if (key&KEY_PLUS) {
			if (m41_control&0x20) {
				m41_control++;
				if (m41_control>0x3f) m41_control=0x1f;
			}else{
				m41_control--;
				if (m41_control==0) m41_control=0x20;
			}
		}
		if (key&KEY_MINUS) {
			if (m41_control&0x20) {
				m41_control--;
				if (m41_control==0x1f) m41_control=0x1;
			}else{
				m41_control++;
				if (m41_control==0x20) m41_control=0x3f;
			}
		}
		draw_time_adj(menu_item);
		break;
	case PRESS_ADJ_SELECT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=PRESS_ADJ_EDIT;
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=LED_MAX_SELECT;
			draw_lcd_max(menu_item);
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=TIME_ADJ_SELECT;
			draw_time_adj(menu_item);
		}
		draw_press_adj(menu_item);
		break;
	case PRESS_ADJ_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=LED_MAX_SELECT;
			draw_lcd_max(menu_item);
		}
		if (key&KEY_PLUS) {
			hop03_correct++;
			if (hop03_correct>999) hop03_correct=-999;
		}
		if (key&KEY_MINUS) {
			hop03_correct--;
			if (hop03_correct<-999) hop03_correct=999;
		}
		draw_press_adj(menu_item);
		break;
	case LED_MAX_SELECT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=LED_MAX_EDIT;
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=LED_MIN_SELECT;
			draw_lcd_min(menu_item);
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=PRESS_ADJ_SELECT;
			draw_press_adj(menu_item);
		}
		draw_lcd_max(menu_item);
		break;
	case LED_MAX_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=LED_MIN_SELECT;
		}
		if (key&KEY_PLUS) {
			if (led_power_max<24) led_power_max++;
			lcd_backlight_on(led_power_max);
		}
		if (key&KEY_MINUS) {
			if (led_power_max>1) led_power_max--;
			lcd_backlight_on(led_power_max);
			if (led_power_min>=led_power_max) {
				led_power_min=led_power_max-1;
				draw_lcd_min(menu_item);
			}
		}
		draw_lcd_max(menu_item);
		break;
	case LED_MIN_SELECT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=LED_MIN_EDIT;
			lcd_backlight_on(led_power_min);
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=LED_TIME_SELECT;
			draw_lcd_time(menu_item);
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=LED_MAX_SELECT;
			draw_lcd_max(menu_item);
		}
		draw_lcd_min(menu_item);
		break;
	case LED_MIN_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=LED_TIME_SELECT;
			draw_lcd_time(menu_item);
			lcd_backlight_on(led_power_max);
		}
		if (key&KEY_PLUS) {
			if (led_power_min<led_power_max-1) led_power_min++;
			lcd_backlight_on(led_power_min);
		}
		if (key&KEY_MINUS) {
			if (led_power_min>0) led_power_min--;
			if (led_power_min) lcd_backlight_on(led_power_min); else lcd_backlight_off();
		}
		draw_lcd_min(menu_item);
		break;
	case LED_TIME_SELECT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=LED_TIME_EDIT;
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=EXIT;
			draw_exit(menu_item);
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=LED_MIN_SELECT;
			draw_lcd_min(menu_item);
		}
		draw_lcd_time(menu_item);
		break;
	case LED_TIME_EDIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			menu_item=EXIT;
			draw_exit(menu_item);
		}
		if (key&KEY_PLUS) {
			led_time++;
		}
		if (key&KEY_MINUS) {
			led_time--;
		}
		draw_lcd_time(menu_item);
		break;
	case EXIT:
		if (key==(KEY_ENTER|KEY_CHANGE)) {
			//			wdt_enable(WDTO_15MS);
			//			for(;;){}
			GLCD_clear();
			display_mode=DISPLAY_PRESSURE;
			eep_save_set();
			menu_item=DRAW_MENU;
			redrawall=1;
		}
		if (key==(KEY_PLUS|KEY_CHANGE)) {
			menu_item=DAY_SELECT;
			draw_date(menu_item);
			draw_exit(menu_item);
		}
		if (key==(KEY_MINUS|KEY_CHANGE)) {
			menu_item=LED_TIME_SELECT;
			draw_lcd_time(menu_item);
			draw_exit(menu_item);
		}
		break;
	default:
		break;
	}
}

uint8_t check_keys() {
	static uint16_t reg=0;
	static uint16_t longkey;
	static uint8_t old_key_status=0;
	static uint16_t double_click=0;

	uint8_t tmp;
	tmp=(~KEY_PIN)&(KEY_ENTER|KEY_PLUS|KEY_MINUS);

	if (double_click) double_click--;

	if (tmp!=old_key_status) {
		if (reg>=100) {
			old_key_status=tmp;
			longkey=0xffff;
			if (tmp==KEY_ENTER) {
				if (double_click==0) {
					double_click=0x3000;
				} else {
					double_click=0;
					return DOUBLE_ENTER;
				}

			}
			return tmp|KEY_CHANGE;
		}else {reg++;}
	}else{
		reg=0;
		if ((tmp&(KEY_ENTER|KEY_PLUS|KEY_MINUS))!=0) {
			if (longkey==0) {
				longkey=0x1fff;
				return tmp;
			}else {
				longkey--;
			}
		}
	}
	return 0;
}
